<template>
  <!-- 监督员上报 -->
  <base-breadcrumb class="personManage-container">
    <searchData :searchData="searchArr" @search="handleSearch"></searchData>
    <a-card :bordered="false">
      <list-table
        ref="table"
        rowKey="id"
        :columns="columns"
        :request="loadData"
        show-size-changer
        show-quick-jumper
        showPagination="auto"
        :rowClassName="getRowClassName"
      >
              <template slot="enable" slot-scope="text, record">
          <span>{{ text === '1' ? '禁用' : text === '0' ? '启用' : '-'}}</span>
        </template>
        <template slot="auditStatus" slot-scope="text, record">
          <status-display :statusCode="text" isApproval />
        </template>
        <template slot="action" slot-scope="text, record">
          <MoreButton>
            <a v-btnPermission="'di_002_add_01'" v-if="!hasSetted(record)" @click="handleUpdate(record, 'add')">监督员设置</a>
            <a v-btnPermission="'di_002_loo_04'" @click="handleUpdate(record, 'look')" v-if="hasSetted(record)">查看</a>
            <a v-btnPermission="'di_002_edi_02'" v-if=" record.auditStatus == 0 && hasSetted(record) || record.auditStatus == 2 || record.auditStatus == 3" @click="handleUpdate(record, 'edit')">编辑</a>
            <a v-btnPermission="'di_002_loo_04'" v-if="record.auditStatus && record.auditStatus != 0" @click="handleUpdate(record, 'auditInfo')">审批信息</a>
            <a v-btnPermission="'di_002_edi_02'" v-if="record.auditStatus == 2 && record.inspPersonnelInfoVO && record.inspPersonnelInfoVO.userId" @click="handleUpdate(record, 'status')">{{ record.enable == 1 ? '启用' : '禁用' }}</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import StatusDisplay from '@/components/StatusDisplay'
import { getSupervisorList, updateSupervisorStatus } from '@/api/disciplineInspection/orgStructure/index.js'
import { packageConfirm } from '@/utils/baseAction.js'

const searchArr = [
  {
    name: '兼职监督员',
    value: 'name',
    type: 'input',
  },
  {
    name: '兼职状态',
    value: 'enable',
    type: 'select',
    data: [
      {
        label: '启用',
        value: '0',
      },
      {
        label: '禁用',
        value: '1',
      },
    ],
  },
  {
    name: '所属分公司',
    valueKey: 'brandOrgId',
    type: SelectSubCompany,
  },
  {
    name: '所属项目',
    keyValue: 'brandOrgId',
    valueKey: 'projectId',
    type: SelectProject,
  },
  {
    name: '入职纪检时间',
    value: 'time',
    type: 'dateRange',
    attr: {
      rangeFormat: ['checkStartTime', 'checkEndTime'],
    },
  },
]

const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    },
  },
  {
    title: '分公司',
    dataIndex: 'brandOrgName',
  },
  {
    title: '项目',
    dataIndex: 'projectName',
  },
  {
    title: '兼职监督员',
    dataIndex: 'name',
  },
  {
    title: '入职纪检时间',
    dataIndex: 'checkTime',
  },
  {
    title: '兼职状态',
    dataIndex: 'enable',
    align: 'center',
    scopedSlots: { customRender: 'enable' },
  },
  {
    title: '审核状态',
    dataIndex: 'auditStatus',
    scopedSlots: { customRender: 'auditStatus' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    align: 'center',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name:'a'+ Date.now(),
  components: {
    STable,
    StatusDisplay,
  },
  data() {
    return {
      searchArr: searchArr,
      columns: columns,
      queryParam: {},
    }
  },
  methods: {
    /**
     * 是否已设置监督员
     */
    hasSetted(record) {
      return record.inspPersonnelId || record.inspPersonnelInfoVO && record.inspPersonnelInfoVO.userId
    },
    /**
     * 获取表格行的样式类（禁用的数据需要置灰）
     */
    getRowClassName(record) {
      if(record.enable === 1 || record.enable === '1') {
        return 'table-row--disabled'
      }
      return ''
    },
    async loadData(parameter) {
      const requestParameters = Object.assign({}, { ...parameter }, { query: this.queryParam })
      return getSupervisorList(requestParameters)
    },
    handleSearch(val = {}) {
      this.queryParam = {
        ...val,
      }
      this.refreshTable()
    },
    refreshTable() {
      this.$refs.table.refresh(true)
    },
    /**
     * 处理数据的相关操作（增删改查）
     */
    handleUpdate(data, pageType) {
      const commonHandler = () => {
        this.$router.push({
          path: '/discipline/orgStructure/supervisorReport/update',
          query: {
            id: data.id || '',
            pageType: pageType,
          },
        })
      }
      const actionMap = {
        add: commonHandler,
        edit: commonHandler,
        look: commonHandler,
        auditInfo: () => {
          this.$bus.$emit('showApprovalDetailsDialog', data.id)
        },
        status: () => {
          packageConfirm({
            cancelText: '否',
            okText: '是',
            content: `你正在${data.enable == 1 ? '启用' : '禁用'}${data.projectName}项目监督员${data.name || ''}，确认该操作吗？`,
            callback: async () => {
              const res = await updateSupervisorStatus({ id: data.id })
              this.$message.success(`${data.enable == 1 ? '启用' : '禁用'}成功`)
              this.refreshTable()
            },
          })
        },
      }
      actionMap[pageType] && actionMap[pageType]()
    },
  },
}
</script>

<style>
</style>